import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/purchase_evaluation_template_odoo_api.dart';
import 'package:flutter_admin/models/odoo/purchase_evaluation_template.dart';
import 'package:flutter_admin/models/odoo/template_tester_mapper.dart';
import 'package:flutter_admin/pages/common/cry/model/order_item_model.dart';
import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/state_manager.dart';
import 'package:syncfusion_flutter_gauges/gauges.dart';

class AddTemplateTesterController extends GetxController {
  var entity = TemplateTesterMapper().obs;
  var templateIdList = <SelectOptionVO>[].obs;
  var deptIdList = <SelectOptionVO>[].obs;
  var deptUserList = <SelectOptionVO>[].obs;

  PageModel templatePage =
      PageModel(orders: [OrderItemModel(column: 'create_date', asc: false)]);
  PurchaseEvaluationTemplate purchaseEvaluationTemplate =
      PurchaseEvaluationTemplate();

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    _loadTemplate();
    _loadDeptData();
    update();
  }

  ///
  /// 获取所有的模板数据
  ///@Author: chendeji
  ///@Date: 2022-05-26 08:59:21
  ///
  Future<void> _loadTemplate() async {
    ResponseBodyApi responseBodyApi =
        await PurchaseEvaluationTemplateOdooApi.page(data: {
      "page": templatePage,
      "domainList": purchaseEvaluationTemplate.toDomainList()
    });
    if (responseBodyApi.success!) {
      var templateIdList = (responseBodyApi.data as PageModel)
          .records
          .map((e) => SelectOptionVO(
              value: '${e['id']}', label: '${e['template_name']}'))
          .toList();
      this.templateIdList.addAll(templateIdList);
      entity.value.supplierId = int.parse(templateIdList[0].value as String);
    }
  }

  ///
  /// 获取所有的部门
  ///@Author: chendeji
  ///@Date: 2022-05-26 08:59:37
  ///
  Future<void> _loadDeptData() async {
    var deptIdList = await DynamicDataApi.getModelSelect(
      model: "hr.department",
      name: "complete_name",
      key: "parent_id",
      operator: "!=",
      val: null,
    );
    var value = deptIdList[0].value as String;
    entity.value.deptId = int.parse(value);
    this.deptIdList.addAll(deptIdList);
  }

  ///
  ///根据部门id获取部门的所有员工
  ///@Author: chendeji
  ///@Date: 2022-05-26 09:14:26
  ///
  Future<void> loadDeptUsers(v) async {
    /**
               * 
               * 选定部门后，需要刷新部门内的用户列表 
               * @Author: chendeji
               * @Date: 2022-05-23 18:32:56
              */
    var deptUserList = await DynamicDataApi.getUserListByDeptID(data: {
      'domainList': [
        ['dept', '=', int.parse(v)],
        ['template', '=', entity.value.supplierId]
      ]
    });
    var value = deptUserList[0].value as String;
    Fluttertoast.showToast(msg: '拿到的用户名数据：$value');
    entity.value.testerId = int.parse(value);
    this.deptUserList.addAll(deptUserList);
  }
}
